package com.dx.mes.service.tool.service;

import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_QUERY;
import static com.dx.mes.common.bean.def.GenericDef.E_FTPMSKMAG_MASK_NOT_EXIST;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_OK;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_MESG_OK;
import static com.dx.mes.common.bean.def.GenericDef.EVT_MASK_MOUNT;
import static com.dx.mes.common.bean.def.GenericDef.EVT_MASK_UNMOUNT;
import static com.dx.mes.common.bean.def.GenericDef.TRX_OUT;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.common.table.ppt.Ret_mask_tool;
import com.dx.mes.common.dao.IRetMaskToolDao;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.tool.service.IFtpmskmagService;
import com.dx.mes.facade.tool.tx.Ftpmskmag.FtpmskmagI;
import com.dx.mes.facade.tool.tx.Ftpmskmag.FtpmskmagIA;
import com.dx.mes.facade.tool.tx.Ftpmskmag.FtpmskmagO;
import com.dx.mes.facade.tool.tx.Ftpmskmag.FtpmskmagOA;

@Service("ftpmskmagService")
@SuppressWarnings({"unchecked"})
public class FtpmskmagService implements IFtpmskmagService{
	
	private static final String TX_NAME = "FTPMSKMAG";
	private LogUtils logUtils;
	
	@Autowired
	private IRetMaskToolDao retMaskToolDao;
	
	@Override
	public String subMainProc(String evt_no, String strInMsg) {
		AppContext.clear();
		AppContext.setCurrServiceName(FtpmskmagService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(FtpmskmagService.class);
		logUtils.info("[InTrx:" + strInMsg + "]");
		
		String outMsg = "";
		FtpmskmagO outTrx = new FtpmskmagO();

		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
		
		try {
			FtpmskmagI inTrx = JacksonUtil.fromJson(strInMsg, FtpmskmagI.class);
			char action_flg = inTrx.getAction_flg().charAt(0);
			switch (action_flg) {
			case ACTION_FLG_QUERY:
				queryByCondition(inTrx,outTrx);
				break;
			case 'A':
				chanageMaskStat(inTrx,outTrx);
				break;
			case 'B':
				mountMaskStat(inTrx,outTrx);
				break;
			default:
				errActionFnc(outTrx);
			}
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				retMaskToolDao.flushCurSession();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}

		} catch (Exception ex) {
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.toString());
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			outMsg = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + outMsg + "]");
		}
		return outMsg;
	}
	private void mountMaskStat(FtpmskmagI inTrx, FtpmskmagO outTrx) {
		FtpmskmagIA iary = inTrx.getIary().get(0);
		String mask_id_fk = iary.getMask_id_fk();
		String mask_stat = iary.getMask_stat();
		String tool_id_fk = iary.getTool_id_fk();
		Ret_mask_tool retMaskTool = retMaskToolDao.get(mask_id_fk);
		if (null == retMaskTool) {
			retMaskTool = new Ret_mask_tool();
			retMaskTool.setMask_id_fk(mask_id_fk);
		}
		retMaskTool.setMask_stat(mask_stat);
		if (!Util.isSpaceCheck(tool_id_fk)) {
			retMaskTool.setTool_id_fk(tool_id_fk);
		}
		retMaskTool.setEvt_cate(EVT_MASK_MOUNT);
		retMaskTool.setEvt_usr(inTrx.getEvt_user());
		retMaskTool.setEvt_timestamp(Util.getCurrentTimestamp());
		retMaskToolDao.saveOrUpdate(retMaskTool);
		
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		
	}
	private void chanageMaskStat(FtpmskmagI inTrx, FtpmskmagO outTrx) {
		FtpmskmagIA iary = inTrx.getIary().get(0);
		String mask_id_fk = iary.getMask_id_fk();
		String mask_stat = iary.getMask_stat();
		String tool_id_fk = iary.getTool_id_fk();
		Ret_mask_tool retMaskTool = retMaskToolDao.get(mask_id_fk);
		if (null == retMaskTool) {
			outTrx.setRtn_code(E_FTPMSKMAG_MASK_NOT_EXIST + "");
			outTrx.setRtn_code("Not found This mask["+mask_id_fk+"]");
			return;
		}
		retMaskTool.setMask_stat(mask_stat);
		if (!Util.isSpaceCheck(tool_id_fk)) {
			retMaskTool.setTool_id_fk(tool_id_fk);
		}
		retMaskTool.setEvt_cate(EVT_MASK_UNMOUNT);
		retMaskTool.setEvt_usr(inTrx.getEvt_user());
		retMaskTool.setEvt_timestamp(Util.getCurrentTimestamp());
		retMaskToolDao.saveOrUpdate(retMaskTool);
		
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}
	private void queryByCondition(FtpmskmagI inTrx, FtpmskmagO outTrx) {
		FtpmskmagIA iary = inTrx.getIary().get(0);
		
		String mask_id = iary.getMask_id_fk();
		String tool_id_fk = iary.getTool_id_fk();
		String mask_stat = iary.getMask_stat();
		String hql = "FROM Ret_mask_tool where 1=1";
		if (null != mask_id) {
			hql += "and mask_id_fk = '"+mask_id+"'";
		}
		if (null != tool_id_fk) {
			hql += "and tool_id_fk = '"+tool_id_fk+"'";
		}
		if (null != mask_stat) {
			hql += "and mask_stat = '"+mask_stat+"'";
		}
		List<Ret_mask_tool> maskList = retMaskToolDao.find(hql);
		List<FtpmskmagOA> oaryList = new ArrayList<FtpmskmagOA>();
		for (Ret_mask_tool ret_mask_tool : maskList) {
				FtpmskmagOA oary = new FtpmskmagOA();
				oary.setMask_id_fk(ret_mask_tool.getMask_id_fk());
				oary.setMask_stat(ret_mask_tool.getMask_stat());
				oaryList.add(oary);
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
	}
	public void errActionFnc(FtpmskmagO outTrx) {
		outTrx.setRtn_code("0000009");
		outTrx.setRtn_mesg("Invalid Action_flg");
	}
}
